#!/usr/bin/python3
# -*- coding: utf8 -*-
# Date   : 2021/02/12
# Author : Kwan
# Email  : allenxian@live.com
# Blog   : https://my.oschina.net/u/4173440

from flask import Blueprint, render_template, redirect, url_for, request, json
from flask_login import login_required, current_user
from flasky.common import admin_required, app_logger
from .axios import AxiosManager


# 系统设置
devices = Blueprint(name='devices', import_name='flasky', url_prefix='/devices', template_folder='views/templates')
DeviceAxios = AxiosManager()


@devices.route('/')
@login_required
@admin_required
def default():
    """
    设备管理
    """
    return redirect(url_for('devices.gate'))


@devices.route('/gate', methods=['POST', 'GET'])
@login_required
@admin_required
def gate():
    """
    闸机管理
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'gate'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：devices.gate 数据：{p_data}")
            return DeviceAxios.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：devices.gate 数据：{p_data} 错误：{e}")
        return DeviceAxios.error('處理錯誤，請聯係管理員')
    # GET
    return render_template('devices/gate.html', **DeviceAxios.page_data_init('gate'))


@devices.route('/camera', methods=['POST', 'GET'])
@login_required
@admin_required
def camera():
    """
    摄像头管理
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'camera'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：devices.camera 数据：{p_data}")
            return DeviceAxios.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：devices.camera 数据：{p_data} 错误：{e}")
        return DeviceAxios.error('處理錯誤，請聯係管理員')
    # GET
    return render_template('devices/camera.html', **DeviceAxios.page_data_init('camera'))


@devices.route('/led', methods=['POST', 'GET'])
@login_required
@admin_required
def led():
    """
    LED显示屏
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'led'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：devices.led 数据：{p_data}")
            return DeviceAxios.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：devices.led 数据：{p_data} 错误：{e}")
        return DeviceAxios.error('處理錯誤，請聯係管理員')
    # GET
    return render_template('devices/led.html', **DeviceAxios.page_data_init('led'))


@devices.route('/cashier', methods=['POST', 'GET'])
@login_required
@admin_required
def cashier():
    """
    收费处
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'cashier'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：devices.cashier 数据：{p_data}")
            return DeviceAxios.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：devices.cashier 数据：{p_data} 错误：{e}")
        return DeviceAxios.error('處理錯誤，請聯係管理員')
    # GET
    return render_template('devices/cashier.html', **DeviceAxios.page_data_init('cashier'))